package com.k52group.vn.micromarket;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.k52group.vn.micromarket.R;
import com.google.gson.Gson;
import com.k52group.vn.micromarket.classes.Profile;
import com.k52group.vn.micromarket.utils.Constant;
import com.k52group.vn.micromarket.utils.HttpRequest;
import com.k52group.vn.micromarket.utils.Utils;

public class SigninActivity extends Activity {
	private EditText etusername, etpassword, etcontrolcode;
	private String username, password;
	private Profile profile = new Profile();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signin);
		etusername = (EditText) findViewById(R.id.EditText03);
		etpassword = (EditText) findViewById(R.id.EditText01);
		etcontrolcode = (EditText) findViewById(R.id.EditText02);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		filldata();
	}

	private void filldata() {
		String data = Utils.getData(Constant.USER_LOGIN, this);
		profile = new Gson().fromJson(data, Profile.class);
		if (profile == null) {
			profile = new Profile();
		} else {
			etusername.setText(profile.getUser_name());
			etpassword.setText(profile.getPassword());
			etcontrolcode.setText(profile.getCtrl_code());
		}
	}

	public void onClick(View v) {
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		if (v.getId() == R.id.Button02) {
			profile = new Profile();
			username = etusername.getText().toString();
			password = etpassword.getText().toString();
			if (username.equals("") || password.equals(""))
				Toast.makeText(this, "Please input username, password !",
						Toast.LENGTH_SHORT).show();
			else
				new LoginTask().execute();
		}
	}

	private class LoginTask extends AsyncTask<String, String, String> {
		ProgressDialog dialog;
		boolean isError = false;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(SigninActivity.this);
			dialog.setMessage("Sign in ...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				HttpRequest request = HttpRequest.post(Constant.URL_LOGIN);
				request.form("username", username);
				request.form("password", password);
				request.form("controlcode", etcontrolcode.getText().toString());
				request.form("dns", Utils.getDeviceID(SigninActivity.this));
				request.form("regId",
						Utils.getData(Constant.GCMID, SigninActivity.this));

				String body = request.body();
				System.out.println("respone " + body);
				profile = new Gson().fromJson(body, Profile.class);
			} catch (Exception e) {
				isError = true;
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			if (isError) {
				Toast.makeText(SigninActivity.this, "Login false",
						Toast.LENGTH_SHORT).show();
			} else {
				if (profile != null) {
					forward();
					Utils.saveData(Constant.USER, new Gson().toJson(profile),
							SigninActivity.this);
					Utils.saveData(Constant.USER_LOGIN,
							new Gson().toJson(profile), SigninActivity.this);
				} else
					Toast.makeText(SigninActivity.this, "Login false",
							Toast.LENGTH_SHORT).show();
			}
		}
	}

	private void forward() {
		Intent it = new Intent(this, SearchActivity.class);
		it.putExtra("data", "");
		startActivity(it);
		finish();
		Toast.makeText(SigninActivity.this, "Success", Toast.LENGTH_SHORT)
				.show();
	}
}
